<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
if($_RQ['op']=='main'){	
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	//查询进入该页面的客户所有订单
	$params=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status']) && $_RQ['status']!=100){
		$params['status']=intval($_RQ['status']);
		$return['status']=intval($_RQ['status']);
	}elseif($_RQ['status']==5){
		$params['status']=array(5,6);
		$return['status']=intval($_RQ['status']);
	}else{
		$return['status']=100;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_qiuyuyue_server_order',$params,array($pindex,$psize),$total,array(),'','id desc');
	if($list){
		foreach($list as &$item){
			$member=model('core/member')->detail($item['mid'],array('nickname','mobile'));
			$serve=pdo_get('jy_qiuyuyue_serve',array('id'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['store']=pdo_get('jy_qiuyuyue_store',array('id'=>$item['store_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['user_name']=$member['nickname'];
			$item['user_mobile']=$member['mobile'];
			$item['serve_title']=$serve['title'];
			$item['serve_image']=$serve['image'];
			$item['plate']=pdo_get('jy_qiuyuyue_serve_plate',array('id'=>intval($item['plate_id']),'uniacid'=>$_SESSION['uniacid']),array('title'));
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$item['serve_typetitle']=$this->serve_types[$item['serve_type']];
			$weekid=date("w",strtotime($item['day']));
			$weekid=$weekid?:7;
			$onetime=pdo_get('jy_qiuyuyue_serve_times',array('onday'=>$item['day'],'serveid'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']));
			if($onetime['serve_time']){
				$serve['serve_time']=$onetime['serve_time'];
			}
			$serve_time=json_decode($serve['serve_time'],true);
			$timeids=str_array($item['timeid']);
			$serve_times=array();
			foreach($timeids as $tv){
				if(intval($tv)){
					$timekey=$tv;
				}else{
					$timekey=substr($tv, 0, -1);
				}
				$fee=$serve_time[intval($timekey)]['fee']?:$serve['price'];
				if($weekid==6 && $serve['week6_addfee']>0){
					$fee +=$serve['week6_addfee'];
				}
				if($weekid==7 && $serve['week7_addfee']>0){
					$fee +=$serve['week7_addfee'];
				}
				if($item['is_ban']){
					$fee=50*$fee/100;
				}
				$serve_times[]=array(
					'times'=>$serve_time[intval($timekey)]['times'],
					'fee'=>$fee,
					'id'=>$tv,
				);
			}
			if($item['serve_time']){
				$item['serve_times']=json_decode($item['serve_time'],true);
			}else{
				$item['serve_times']=$serve_times;
			}			
			$item['ordersnurl']=custom_url('jy_qiuyuyue/manage.hexiao//ordersn='.$item['ordersn'],true);
			if($item['status']==1){
				$item['can_refund']=1;
			}
			$item['statustitle']=$this->order_status[$item['status']];
			$item['users']=$item['users']?json_decode($item['users'],true):array();
		}
	}
	$return['list']=$list;
	set_html('title','订单列表');
	exi($return);
}
//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$member=model('core/member')->detail($order['mid'],array('nickname','mobile'));
			$serve=pdo_get('jy_qiuyuyue_serve',array('id'=>$order['serve_id'],'uniacid'=>$_SESSION['uniacid']));
			$order['store']=pdo_get('jy_qiuyuyue_store',array('id'=>$order['store_id'],'uniacid'=>$_SESSION['uniacid']));
			$order['user_name']=$member['nickname'];
			$order['user_mobile']=$member['mobile'];
			$order['serve_title']=$serve['title'];
			$order['serve_image']=$serve['image'];
			$order['plate']=pdo_get('jy_qiuyuyue_serve_plate',array('id'=>intval($order['plate_id']),'uniacid'=>$_SESSION['uniacid']),array('title'));
			$order['createtime']=date('Y-m-d H:i:s', $order['createtime']);
			$order['serve_typetitle']=$this->serve_types[$order['serve_type']];
			$weekid=date("w",strtotime($order['day']));
			$weekid=$weekid?:7;
			$onetime=pdo_get('jy_qiuyuyue_serve_times',array('onday'=>$order['day'],'serveid'=>$order['serve_id'],'uniacid'=>$_SESSION['uniacid']));
			if($onetime['serve_time']){
				$serve['serve_time']=$onetime['serve_time'];
			}
			$serve_time=json_decode($serve['serve_time'],true);
			$timeids=str_array($order['timeid']);
			$serve_times=array();
			$totalprice=0;
			foreach($timeids as $tv){
				$fee=$serve_time[intval($tv)]['fee']?:$serve['price'];
				if($weekid==6 && $serve['week6_addfee']>0){
					$fee +=$serve['week6_addfee'];
				}
				if($weekid==7 && $serve['week7_addfee']>0){
					$fee +=$serve['week7_addfee'];
				}
				if($order['is_ban']){
					$fee=50*$fee/100;
				}
				$serve_time[intval($tv)]['fee']=$fee;
				$serve_times[]=$serve_time[intval($tv)];
			}
			if($order['serve_time']){
				$order['serve_times']=json_decode($order['serve_time'],true);
			}else{
				$order['serve_times']=$serve_times;
			}
	//定义订单状态名称
	if($order['status']==0){
		$order['statustitle']='预约待支付';
		$order['statusdesc']='超时未支付，订单将自动取消';
	}elseif($order['status']==1){
		$order['statustitle']='预约成功';
		$order['statusdesc']='请您留意订单进度';
	}elseif($order['status']==2){
		$order['statustitle']='预约已确认';
		$order['statusdesc']='欢迎下次光临!';
	}elseif($order['status']==3){
		$order['statustitle']='预约已取消';
		$order['statusdesc']='欢迎下次光临！';
	}elseif($order['status']==4){
		$order['statustitle']='预约已完成';
		$order['statusdesc']='欢迎下次光临！';
	}elseif($order['status']==5){
		$order['statustitle']='预约退款中';
		$order['statusdesc']='请您留意退款进度';
	}elseif($order['status']==6){
		$order['statustitle']='预约已退款';
		$order['statusdesc']='感谢您的支持和理解，欢迎下次光临！';
	}else{
		$order['statustitle']='预约已拒绝退款';
		$order['statusdesc']='感谢您的支持和理解，欢迎下次光临！';
	}
	$order['ordersnurl']=custom_url('jy_qiuyuyue/manage.hexiao//ordersn='.$order['ordersn'],true);
	$order['paytype']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$order['id'],'plugin'=>'jy_qiuyuyue'),'pay_type');
	$order['paytype']=get_pay_types()[$order['paytype']];
	$order['users']=$order['users']?json_decode($order['users'],true):array();
	$return['order']=$order;
	$return['member']=$member;
	$return['serve']=$serve;
	set_html('title','订单详情');
	exi($return);
}
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_qiuyuyue_server_order',array('status'=>'4'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//确认订单后，计算员工佣金,存入佣金日志表，并更新员工表员工总佣金
	$worker_id=pdo_getcolumn('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'worker_id');
	$pay=pdo_getcolumn('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'pay');
	$lev_id=pdo_getcolumn('jy_qiuyuyue_store_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id),'lev');
	$rate=pdo_getcolumn('jy_qiuyuyue_lev',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lev_id),'rate');
	if($rate){
		$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'store_id'=>pdo_getcolumn('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'store_id'),
		'worker_id'=>$worker_id,
		'order_id'=>$_RQ['id'],
		'money'=>$pay*$rate/100,
		'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_qiuyuyue_commission_log',$insert);
		$commission=pdo_getcolumn('jy_qiuyuyue_store_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id),'commission');
		$commission=$commission+$pay*$rate/100;
		pdo_update('jy_qiuyuyue_store_worker',array('commission'=>$commission),array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id));
	}
	
	exi('确认预定成功');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除预定成功');
}
if($_RQ['op']=='pay'){
	$post=get_postdata();
	$id=$post['id'];
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	//已支付，已确认，已完成订单数
	$order=pdo_get('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$num=pdo_count('jy_qiuyuyue_server_order',array('timeid'=>$order['timeid'],'uniacid'=>$_SESSION['uniacid'],'store_id'=>$order['store_id'],'day'=>$order['day'],'serve_id'=>$order['serve_id'],'plate_id'=>$order['plate_id'],'status'=>array(1,2,4)));
	if($num){
		exi('该场次已经被预约了！','error');
	}
	$pay_id=pdo_getcolumn('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'pay_id');
	exi($pay_id);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_qiuyuyue_server_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_qiuyuyue_server_order',array('status'=>1,'pay'=>$payresult['fee']),array('id'=>$order['id']));
	exi('支付成功','success','jy_qiuyuyue/server_order/list/status=1');
}